package com.hzhk.happyrent.ui.activity.setting;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.UpdateModel;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.HomeActivity;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.personal.AboutHappyRentActivity;
import com.hzhk.happyrent.ui.view.UpdateView;
import com.hzhk.happyrent.ui.activity.personal.BindPhoneActivity;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import com.tamic.novate.Throwable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.about_happyrent)
    RelativeLayout aboutHappyrent;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.login_out)
    Button loginOut;
    @BindView(R.id.version_check)
    RelativeLayout versionCheck;
    private Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText("设置");
        version.setText(API.VERSIONNAME);
        if(TextUtils.isEmpty(MyPerference.getInstance().getUserId())){
            loginOut.setVisibility(View.GONE);
        }else{
            loginOut.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.activity_titlebar_left
            , R.id.about_happyrent
            , R.id.login_out
            , R.id.version_check})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.about_happyrent:
                Intent intent = new Intent(this, AboutHappyRentActivity.class);
                startActivity(intent);
                break;
            case R.id.login_out:
                // TODO: 2018/1/6 测试保留，后期删除
//                deleteData();
//                SystemClock.sleep(1000);
                MyPerference.getInstance().clearUserInfo();
                Toast.makeText(this, "退出成功", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.version_check:
                updateCheck();
                break;
        }
    }

    private void updateCheck() {
        HashMap<String,Object> params=new HashMap<>();
        params.put("type","1");
        HttpManager.getInstance().get(API.APP_UPGRADE, params, new MyStringResponse(this) {
            @Override
            public void onNext(Object tag, String response) {
                UpdateModel updateModel=new Gson().fromJson(response,UpdateModel.class);
                if(0==updateModel.getErrCode()){
                    if("1".equals(updateModel.getData().getStatus())&& API.VERSIONCODE<Integer.parseInt(updateModel.getData().getAndroidVersion())){
                        new UpdateView(SettingActivity.this).build(updateModel);
                    }else{
                        Toast.makeText(SettingActivity.this, "已经是最新版本", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });
    }

    private void deleteData() {
        Map<String, Object> params = new HashMap<>();
        params.put("phone",MyPerference.getInstance().getUserPhone() );
        log.debug("phone = " + MyPerference.getInstance().getUserPhone());
        Log.d("zzc","phone = " + MyPerference.getInstance().getUserPhone());
        HttpManager.getInstance().perform_post(API.DEL_OPENID, params, new MyStringResponse(this) {
            @Override
            public void onNext(Object tag, String response) {
                log.debug("response" + response);
                Toast.makeText(SettingActivity.this, response, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Object tag, Throwable e) {
                Toast.makeText(SettingActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {
                Toast.makeText(SettingActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
